package com.touch.core.job;
import java.io.File;
import java.util.Vector;

import com.touch.core.Constants;
import com.touch.core.script.ByteArray;
import com.touch.core.script.ByteDecoder;
import com.touch.core.util.IOTools;
public class Job extends ByteDecoder implements Cloneable
{
	public String name;
	/** The archclass is the class that this class belongs to*/
	public Vector<String> archclass = new Vector<String>();
	public int levelReq;
	/** locked = true: the class cannot be chosen normally */
	public boolean locked;
	public int maleSprite, femaleSprite;
	
	//base states
	public int baseSTR;
	public int baseDEX;
	public int baseINT;
	public int baseSPT;	
	
	//start positions
	public int map;
	public int x,y,z;
	
	public static Job loadClass(String classname)
	{
		File avatarDat = new File(classname);		
		if(!avatarDat.exists())		
			return null;
		Job c = new Job();
		byte[] data = IOTools.readFileAsBytes(classname);
		c.decode(data);
		return c;
	}
	public void save()
	{
		String path = "Classes/" + name + ".dat";
		this.saveAs(path);
	}
	public void saveAs(String path)
	{
		ByteArray data = new ByteArray();
		
		this.encodeArchClass(data);
		this.encodeName(data);
		this.encodeBaseStats(data);
		this.encodeLevelRequirement(data);
		this.encodeLocation(data);
		this.encodeLocked(data);
		this.encodeName(data);
		this.encodeSprite(data);
		
		IOTools.writeBytesToFile(path, data.toArray());
	}
	private void encodeName(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(JobCode.NAME.getValue());
		data.appendTo(name);
		data.appendTo(Constants.com_END);
	}
	private void parseName(ByteArray data)
	{
		this.name = data.toString();
	}
	private void encodeArchClass(ByteArray data)
	{
		for(String ac: archclass)
		{
			data.appendTo(Constants.com_START);
			data.appendTo(JobCode.ARCHCLASS.getValue());
			data.appendTo(ac);
			data.appendTo(Constants.com_END);
		}
	}
	private void parseArchClass(ByteArray data)
	{
		archclass.add(data.toString());
	}
	private void encodeLocked(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(JobCode.LOCKED.getValue());
		if(locked)
			data.appendTo((byte)0x1);
		else
			data.appendTo((byte)0x0);
		data.appendTo(Constants.com_END);
	}
	private void parseLocked(ByteArray data)
	{
		if(data.toByte() == 0)
			locked = false;
		else
			locked = true;
	}
	private void encodeLevelRequirement(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(JobCode.LEVELREQ.getValue());
		data.appendTo(levelReq);
		data.appendTo(Constants.com_END);
	}
	private void parseLevelRequirement(ByteArray data)
	{
		this.levelReq = data.toInt();
	}
	private void encodeSprite(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(JobCode.SPRITE.getValue());
		data.appendTo(maleSprite);
		data.appendTo(femaleSprite);
		data.appendTo(Constants.com_END);
	}
	private void parseSprite(ByteArray data)
	{
		maleSprite = data.subArray(0, 4).toInt();
		femaleSprite = data.subArray(4, 8).toInt();
	}
	private void encodeBaseStats(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(JobCode.STATS.getValue());
		data.appendTo(baseSTR);
		data.appendTo(baseDEX);
		data.appendTo(baseINT);
		data.appendTo(baseSPT);
		data.appendTo(Constants.com_END);
	}
	private void parseBaseStats(ByteArray data)
	{
		this.baseSTR = data.subArray(0, 4).toInt();
		this.baseDEX = data.subArray(4, 8).toInt();
		this.baseINT = data.subArray(8, 12).toInt();
		this.baseSPT = data.subArray(12, 16).toInt();
	}
	private void encodeLocation(ByteArray data)
	{
		data.appendTo(Constants.com_START);
		data.appendTo(JobCode.LOCATION.getValue());
		data.appendTo(map);
		data.appendTo(x);
		data.appendTo(y);
		data.appendTo(z);
		data.appendTo(Constants.com_END);
	}
	private void parseLocation(ByteArray data)
	{
		map = data.subArray(0, 4).toInt();
		x	= data.subArray(4, 8).toInt();
		y	= data.subArray(8, 12).toInt();
		z	= data.subArray(12, 16).toInt();
	}
	@Override
	public void parser(ByteArray command) 
	{	
		JobCode code = JobCode.getClassCode(command.toArray()[0]);
		command = command.subArray(1, command.length());
		
		if(code == JobCode.ARCHCLASS)
			this.parseArchClass(command);
		else if(code == JobCode.LEVELREQ)
			this.parseLevelRequirement(command);
		else if(code == JobCode.LOCATION)
			this.parseLocation(command);
		else if(code == JobCode.LOCKED)
			this.parseLocked(command);
		else if(code == JobCode.NAME)
			this.parseName(command);
		else if(code == JobCode.SPRITE)
			this.parseSprite(command);
		else if(code == JobCode.STATS)
			this.parseBaseStats(command);
	}
	public Job clone()
	{
		final Job clone = new Job();
		
		for(String cloneArchClass: this.archclass)
			clone.archclass.add(cloneArchClass);
		clone.baseDEX = this.baseDEX;
		clone.baseINT = this.baseINT;
		clone.baseSPT = this.baseSPT;
		clone.baseSTR = this.baseSTR;
		clone.femaleSprite = this.femaleSprite;
		clone.levelReq = this.levelReq;
		clone.locked = this.locked;
		clone.maleSprite = this.maleSprite;
		clone.map = this.map;
		clone.x = this.x;
		clone.y = this.y;
		clone.z = this.z;
		
		return clone;
	}
}
